# -*- coding: UTF-8 -*-
"""
@author: tgy
@file: server_demo01.py
@time: 2021/02/19
"""
import threading
import socket


class Server:
    ...

    def __init__(self, address, call_back):
        ...
        self.call_back = call_back
        self.socket = socket.socket(family=socket.AF_INET, type=socket.SOCK_STREAM)
        self.socket.bind(address)
        self.socket.listen(1024)

    def accept(self):
        ...
        while True:
            ...
            client, client_msg = self.socket.accept()
            print(client_msg)
            thread = threading.Thread(target=self.call_back, args=(client, client_msg))
            thread.setDaemon(True)
            thread.start()

class CallBack:
    ...

    def __call__(self, client, client_msg):
        ...
        print(client_msg)
        while True:
            ...
            try:

                recv_msg = client.recv(1024).decode('utf-8')
                print(recv_msg)
                client.send(b'hello')
            except Exception as e:
                ...
                print(e)
                client.close()
                break


def run():
    ...
    print('1111')
    yield
    print('22222')

if __name__ == '__main__':
    pass
    # server = Server(address=('0.0.0.0', 8090), call_back=CallBack())
    # server.accept()
    g = run()
    next(g)
